package com.arcsoft.idcardveridemo.others;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.support.annotation.RequiresApi;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.arcsoft.idcardveridemo.MainActivity;
import com.arcsoft.idcardveridemo.R;


public class WebViewHelper {
    private final String  webViewUrl="https://www.baidu.com";


    public  MainActivity mainActivity;

    public  WebView mWebView;

    Handler handler;

    public WebViewHelper(WebView mWebView, MainActivity mainActivity, Handler handler) {
        this.mainActivity=mainActivity;
        this.mWebView=mWebView;
        this.handler=handler;

    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
    public WebView initView() {
        mWebView = mainActivity.findViewById(R.id.web_view);

        // 启用javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

        mWebView.getSettings().setDomStorageEnabled(true);
        mWebView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = mainActivity.getCacheDir().getAbsolutePath();
        mWebView.getSettings().setAppCachePath(appCachePath);
        mWebView.getSettings().setAllowFileAccess(true);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);

        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                Log.i(TAG, "shouldOverrideUrlLoading::url = " + url);
                mWebView.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, final String url) {
                super.onPageFinished(view, url);
            }
        });

        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);


        mWebView.setWebChromeClient(new WebChromeClient());

        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);

//        mWebView.getSettings().set


//        mWebView.addJavascriptInterface(new UboFaceJs(handler), UboFaceJs.JSObject);
//        mWebView.addJavascriptInterface(new UboPrintJs(handler), UboPrintJs.JSObject);

        mWebView.loadUrl(webViewUrl);

        return mWebView;

    }
}
